package com.carolouvindam;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.carolouvindam.Model.Address;
import com.carolouvindam.Model.Client;
import com.carolouvindam.Model.Order;
import com.carolouvindam.Model.Product;
import com.carolouvindam.database.DataSource;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FormActivity extends Activity {

    public static List<Product> TemporaryOrderList = new ArrayList<Product>();

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.createform);
        getSpinner();
    }

    //Récupère la commande
    private Order getOrder() {
        Order order = new Order(null,
                new Client(
                        getClientName(),
                        getClientFirstName(),
                        getClientNumber()
                ),
                new Address(
                        getAddressNumber(),
                        getAddressStreet(),
                        getCity(),
                        getPostalCode()
                ),
                this.TemporaryOrderList,
                new Date()
        );
        return order;
    }

    //Définit le contenu du Spinner
    private void getSpinner() {

        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        List<String> str = new ArrayList<String>();
        str.add("A cake ?");
        str.addAll(Application.getCakesList(Application.CAKES));
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        isSpinnerInitiated = false;
        spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
    }

    private boolean isSpinnerInitiated = false;

    public class MyOnItemSelectedListener implements AdapterView.OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {

            Product product = Application.getCakeFromName(parent.getItemAtPosition(pos).toString());
            if (isSpinnerInitiated == false) {
                isSpinnerInitiated = true;
            } else {
                if (product == null) {
                    Toast.makeText(parent.getContext(), "The cake is " +
                            "UNKNOWN !!!", Toast.LENGTH_LONG).show();
                } else {
                    FormActivity.TemporaryOrderList.add(product);

                    Toast.makeText(parent.getContext(), "You've just added a " +
                            parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                }
                Spinner spinner = (Spinner) view.getParent();
                spinner.setSelection(0);
                isSpinnerInitiated = false;
                updateCakeListDisplay();
            }
        }

        public void onNothingSelected(AdapterView parent) {
            // Ne fait rien
        }
    }

    //Affiche la liste de gateaux sélectionnés
    private void updateCakeListDisplay()  {
        String str = null;
        
        for(Product p : TemporaryOrderList) {
            if(str == null)
            {
                str = p.getName();
            }
            else{
                str = str + " ; " + p.getName();
            }
        }
        TextView txtView = (TextView) findViewById(R.id.ProductDisplay);
        txtView.setText(str);
    }


    //Récupère le nom du Client
    private String getClientName() {
        EditText editText = (EditText) findViewById(R.id.Name);
        return editText.getText().toString();
    }

    //Récupère le prénom du Client
    private String getClientFirstName() {
        EditText editText = (EditText) findViewById(R.id.FirstName);
        return editText.getText().toString();
    }

    //Récupère le numéro du Client
    private String getClientNumber() {
        EditText editText = (EditText) findViewById(R.id.Number);
        return editText.getText().toString();
    }

    //Récupère le numéro d'adresse du Client
    private String getAddressNumber() {
        EditText editText = (EditText) findViewById(R.id.AddressNumber);
        return editText.getText().toString();
    }

    private String getAddressStreet() {
        EditText editText = (EditText) findViewById(R.id.Street);
        return editText.getText().toString();
    }

    //Récupère la rue du Client
    private String getCity() {
        EditText editText = (EditText) findViewById(R.id.City);
        return editText.getText().toString();
    }

    //Récupère le code postal du Client
    private String getPostalCode() {
        EditText editText = (EditText) findViewById(R.id.PostalCode);
        return editText.getText().toString();
    }


    //Enregistre la commande
    public void save(View v) {
        DataSource bdd = new DataSource(this);
        bdd.open();
        bdd.insert(this.getOrder());
        Toast.makeText(this, "Order Saved", Toast.LENGTH_SHORT).show();
        bdd.close();
        TemporaryOrderList = new ArrayList<Product>();

        Intent intent = new Intent(this, OrderActivity.class);
        startActivity(intent);
    }


    //Annule la commande
    public void cancel(View view) {
        Intent intent = new Intent(this, OrderActivity.class);
        startActivity(intent);
    }

}


